/*     */ package com.zimbra.cs.service.account;
/*     */ 
/*     */ import com.google.common.base.Splitter;
/*     */ import com.google.common.collect.Sets;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AccountConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Entry;
/*     */ import com.zimbra.cs.account.Group;
/*     */ import com.zimbra.cs.account.Group.GroupOwner;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.ldap.entry.LdapDistributionList;
/*     */ import com.zimbra.cs.account.ldap.entry.LdapDynamicGroup;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import com.zimbra.soap.account.type.MemberOfSelector;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class GetAccountDistributionLists
/*     */   extends AccountDocumentHandler
/*     */ {
/*     */   public Element handle(Element request, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/*  43 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  44 */     Account acct = getRequestedAccount(zsc);
/*  45 */     Provisioning prov = Provisioning.getInstance();
/*     */     
/*  47 */     if (!canAccessAccount(zsc, acct)) {
/*  48 */       throw ServiceException.PERM_DENIED("can not access account");
/*     */     }
/*     */     
/*  51 */     boolean needOwnerOf = request.getAttributeBool("ownerOf", false);
/*  52 */     MemberOfSelector needMemberOf = MemberOfSelector.fromString(request.getAttribute("memberOf", MemberOfSelector.directOnly.name()));
/*     */     
/*     */ 
/*  55 */     Iterable<String> needAttrs = Splitter.on(',').trimResults().split(request.getAttribute("attrs", ""));
/*     */     
/*     */ 
/*  58 */     Set<Group> ownerOf = null;
/*  59 */     List<Group> memberOf = null;
/*  60 */     HashMap<String, String> via = new HashMap();
/*     */     
/*  62 */     if (needOwnerOf) {
/*  63 */       ownerOf = Group.GroupOwner.getOwnedGroups(acct);
/*     */     }
/*     */     
/*  66 */     if (MemberOfSelector.none != needMemberOf) {
/*  67 */       memberOf = prov.getGroups(acct, MemberOfSelector.directOnly == needMemberOf, via);
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*  73 */     Set<Entry> combined = Sets.newHashSet();
/*  74 */     Set<String> combinedIds = Sets.newHashSet();
/*  75 */     Set<String> ownerOfGroupIds = Sets.newHashSet();
/*  76 */     Set<String> memberOfGroupIds = Sets.newHashSet();
/*     */     
/*  78 */     if (ownerOf != null) {
/*  79 */       for (Group group : ownerOf) {
/*  80 */         String groupId = group.getId();
/*  81 */         ownerOfGroupIds.add(groupId);
/*     */         
/*  83 */         if (!combinedIds.contains(groupId)) {
/*  84 */           combined.add(group);
/*  85 */           combinedIds.add(groupId);
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*  90 */     if (memberOf != null) {
/*  91 */       for (Group group : memberOf) {
/*  92 */         String groupId = group.getId();
/*  93 */         memberOfGroupIds.add(groupId);
/*     */         
/*  95 */         if (!combinedIds.contains(groupId)) {
/*  96 */           combined.add(group);
/*  97 */           combinedIds.add(groupId);
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 103 */     List<Entry> sortedGroups = Entry.sortByDisplayName(combined, acct.getLocale());
/*     */     
/* 105 */     Element response = zsc.createElement(AccountConstants.GET_ACCOUNT_DISTRIBUTION_LISTS_RESPONSE);
/*     */     
/* 107 */     for (Entry entry : sortedGroups) {
/* 108 */       Group group = (Group)entry;
/*     */       
/* 110 */       Element eDL = response.addElement("dl");
/* 111 */       eDL.addAttribute("name", group.getName());
/* 112 */       if (group.isDynamic()) {
/* 113 */         eDL.addAttribute("ref", ((LdapDynamicGroup)group).getDN());
/*     */       } else {
/* 115 */         eDL.addAttribute("ref", ((LdapDistributionList)group).getDN());
/*     */       }
/* 117 */       eDL.addAttribute("id", group.getId());
/* 118 */       eDL.addAttribute("d", group.getDisplayName());
/* 119 */       eDL.addAttribute("dynamic", group.isDynamic());
/*     */       
/* 121 */       boolean isOwner = ownerOfGroupIds.contains(group.getId());
/*     */       
/* 123 */       if (needOwnerOf) {
/* 124 */         eDL.addAttribute("isOwner", isOwner);
/*     */       }
/*     */       
/* 127 */       if (MemberOfSelector.none != needMemberOf) {
/* 128 */         boolean isMember = memberOfGroupIds.contains(group.getId());
/* 129 */         eDL.addAttribute("isMember", isMember);
/*     */         
/* 131 */         if (isMember) {
/* 132 */           String viaDl = (String)via.get(group.getName());
/* 133 */           if (viaDl != null) {
/* 134 */             eDL.addAttribute("via", viaDl);
/*     */           }
/*     */         }
/*     */       }
/*     */       
/* 139 */       Set<String> returnAttrs = GetDistributionList.visibleAttrs(needAttrs, isOwner);
/* 140 */       if (!returnAttrs.isEmpty()) {
/* 141 */         GetDistributionList.encodeAttrs(group, eDL, returnAttrs);
/*     */       }
/*     */     }
/* 144 */     return response;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/account/GetAccountDistributionLists.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */